const { Send } = require("../utils/response");
const Gallery = require("../models/gallery");
const Collection = require("../models/collection");

class GalleryController {
    static async add(req, res, next) {
        try {
            const gallery = await Gallery.create({
                name: req.body.name,
                thumbnail: req.body.thumbnail,
                categoryId: req.body.categoryId
            });
            if (!gallery) {
                return Send(res, {
                    code: -1,
                    message: "图库添加失败"
                });
            }
            Send(res, {
                message: "图库添加成功",
                data: {
                    id: gallery._id
                }
            });
        } catch (err) {
            next(err);
        }
    }
    static async delete(req, res, next) {
        try {
            const { deletedCount } = await Gallery.deleteOne({
                _id: req.params.id
            });
            if (deletedCount === 0) {
                return Send(res, {
                    message: "该图库不存在"
                });
            }
            return Send(res, {
                message: "图库删除成功",
                data: req.params.id
            });
        } catch (err) {
            next(err);
        }
    }
    static async list(req, res, next) {
        try {
            const list = await Gallery.find({})
                .select("+name +createdAt +isActive")
                .populate("categoryId", "name");
            return Send(res, {
                message: "获取成功",
                data: list
            });
        } catch (error) {
            next(error);
        }
    }
    static async update(req, res, next) {
        try {
            if (req.body && req.body.name) {
                const isExist = await Gallery.findOne({ name: req.body.name });
                if (isExist) {
                    return Send(res, {
                        code: -1,
                        message: "该图库已存在"
                    });
                }
            }
            const gallery = await Gallery.findByIdAndUpdate(
                req.params.id,
                req.body
            );
            if (!gallery) {
                return Send(res, {
                    code: -1,
                    message: "该图库不存在"
                });
            }
            Send(res, {
                message: "修改成功",
                data: gallery._id
            });
        } catch (error) {
            next(error);
        }
    }
    static async collections(req, res, next) {
        try {
            const gallery = await Gallery.findOne({
                _id: req.params.id
            });
            if (!gallery) {
                return Send(res, {
                    code: -1,
                    message: "该图库不存在"
                });
            }
            const collections = await Collection.find({
                galleryId: req.params.id
            });
            Send(res, {
                message: "获取图库下的图集列表成功",
                data: collections
            });
        } catch (error) {
            next(error);
        }
    }
}

module.exports = GalleryController;
